package com.zhang.util;

import com.zhang.pojo.SaleBean;
import com.zhang.service.IOrderService;
import com.zhang.service.impl.OrderServiceImpl;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.awt.*;
import java.util.List;

//https://blog.csdn.net/tkd03072010/article/details/7366056
//@Component
public class ChartUtil {
//    @Autowired
//    private  IOrderService orderService;

    /**
     * 创建数据集合 
     * @return dataSet 
     */  
    public  CategoryDataset createDataSet(List<SaleBean> saleBeans ) {
        // 实例化DefaultCategoryDataset对象  
        DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
        // 向数据集合中添加数据
        for(SaleBean saleBean:saleBeans) {
        	dataSet.addValue(saleBean.getMonth(), "销售额详情", saleBean.getMoney()+"");
        }
        return dataSet;  
    }  
    /** 
     * 创建JFreeChart对象 
     * @return chart 
     */
    public  JFreeChart createChart(int year) {
        OrderServiceImpl orderService = new OrderServiceImpl();
        System.out.println(orderService.findAll(2021));
        List<SaleBean> all = orderService.findAll(2021);
        StandardChartTheme standardChartTheme = new StandardChartTheme("CN"); //创建主题样式  
        standardChartTheme.setExtraLargeFont(new Font("隶书", Font.BOLD, 20)); //设置标题字体
        standardChartTheme.setRegularFont(new Font("宋体", Font.PLAIN, 15)); //设置图例的字体
        standardChartTheme.setLargeFont(new Font("宋体", Font.PLAIN, 15)); //设置轴向的字体
        ChartFactory.setChartTheme(standardChartTheme);//设置主题样式  
        // 通过ChartFactory创建JFreeChart  
        JFreeChart chart = ChartFactory.createBarChart3D(  
                "销售额统计", //图表标题
                "年份", //横轴标题
                "销售额",//纵轴标题
                createDataSet(all),//数据集合
                PlotOrientation.VERTICAL, //图表方向  
                true,//是否显示图例标识  
                false,//是否显示tooltips  
                false);//是否支持超链接  
        return chart;  
    }
}  